const TokenKey = 'accessToken'
const USER_INFO_KEY = 'userInfo'

export function getToken() {
  return localStorage.getItem(TokenKey)
}

export function setToken(token) {
  return localStorage.setItem(TokenKey, token)
}

export function removeToken() {
  return localStorage.removeItem(TokenKey)
}

export function getUserInfo() {
  const info = localStorage.getItem(USER_INFO_KEY)
  return info ? JSON.parse(info) : null
}

export function setUserInfo(info) {
  return localStorage.setItem(USER_INFO_KEY, JSON.stringify(info))
}

export function removeUserInfo() {
  return localStorage.removeItem(USER_INFO_KEY)
}

export function getUser() {
  const userStr = localStorage.getItem(USER_INFO_KEY)
  return userStr ? JSON.parse(userStr) : null
}

export function setUser(user) {
  return localStorage.setItem(USER_INFO_KEY, JSON.stringify(user))
}

export function removeUser() {
  return localStorage.removeItem(USER_INFO_KEY)
}

// 获取用户角色ID
export function getRoleId() {
  const userInfo = getUserInfo()
  return userInfo ? userInfo.roleId : null
}

// 判断是否为管理员
export function isAdmin() {
  return getRoleId() === 1
}

// 判断是否为商家用户
export function isMerchant() {
  return getRoleId() === 2
}

// 判断是否有权限
export function hasPermission(requiredRoleId) {
  const roleId = getRoleId()
  return roleId === requiredRoleId
} 